var app = getApp();
Page({
  data: {
	  cdnUrl: '',
    userInfo: null,
		/* 自定义导航栏参数 */
		loading: false,
		color: '#FFFFFF',
		background: '#FFFFFF',
		show: true,
		animated: false,
		back: true,
		title: "骑手入驻",
    
    /* 定义表单数据*/
    form:{
      apply_name: '',//手机号码
      apply_mobile: '',//真实姓名
      IDcard: '',//身份证号码
      imageback: '',//身份证反面图片
      imagefront: '',//身份证正面图片
      rider_type: '',//申请骑手类型
      prefecture: '',//所在区域
    },
    applySuccess: true
  },

  onLoad: function (options) {
		var that = this;
    //使用缓存保存基本信息
    var mobile = wx.getStorageSync("mobile");
    var realName = wx.getStorageSync("realName");
    var idcard = wx.getStorageSync("idcard");
    var rider_type = wx.getStorageSync("rider_type");
    var prefecture = wx.getStorageSync("prefecture");
    if(!realName){
      console.log('不存在');
    }
    if(options.hasOwnProperty('mobile')){
      if(options.mobile=='') {
      	app.error('请输入手机号');
      	wx.navigateTo({
      	  url: "/page/rider_apply/step1"
      	})
      }else if(options.mobile.length != 11){
        app.error('手机号格式错误');
        wx.navigateTo({
          url: "/page/rider_apply/step1"
        })
      }
    }else{
      app.error('基本信息不能为空');
      setTimeout(function () { 
        wx.navigateTo({
          url: "/page/rider_apply/step1"
        })
      }, 1500);
      return false;
    }
    if(options.hasOwnProperty('realName')){
      if(options.realName=='') {
      	app.error('请输入真实姓名');
      	wx.navigateTo({
      	  url: "/page/rider_apply/step1"
      	})
      }
    }else{
      app.error('基本信息不能为空');
      setTimeout(function () { 
        wx.navigateTo({
          url: "/page/rider_apply/step1"
        })
      }, 1500);
      return false;
    }
    if(options.hasOwnProperty('idcard')){
      if(options.idcard=='') {
      	app.error('请输入真实姓名');
      	wx.navigateTo({
      	  url: "/page/rider_apply/step1"
      	})
      }
    }else{
      app.error('基本信息不能为空');
      setTimeout(function () { 
        wx.navigateTo({
          url: "/page/rider_apply/step1"
        })
      }, 1500);
      return false;
    }
    if(options.hasOwnProperty('rider_type')){
      if(options.rider_type=='') {
      	app.error('请先选择骑手类型');
      	wx.navigateTo({
      	  url: "/page/rider_apply/step1"
      	})
      }
    }else{
      app.error('请先选择骑手类型');
      setTimeout(function () { 
        wx.navigateTo({
          url: "/page/rider_apply/step1"
        })
      }, 1500);
      return false;
    }
    if(options.hasOwnProperty('prefecture')){
      if(options.prefecture=='') {
      	app.error('请先选择所在区域');
      	wx.navigateTo({
      	  url: "/page/rider_apply/step1"
      	})
      }
    }else{
      app.error('请先选择所在区域');
      setTimeout(function () { 
        wx.navigateTo({
          url: "/page/rider_apply/step1"
        })
      }, 1500);
      return false;
    }
    setTimeout(function () {
      if (!app.globalData.userInfo) {
        app.check(function(ret){}, "/page/rider_apply/step2?mobile="+mobile+"&realName="+realName+"&idcard="+idcard+"&rider_type="+rider_type+"&prefecture="+prefecture);
      }
    }, 1)
    // wx.removeStorageSync('mobile');
    // wx.removeStorageSync('realName');
    // wx.removeStorageSync('idcard');
    that.setData({
      cdnUrl: app.cdnUrl,
      userInfo: app.globalData.userInfo,
      ['form.apply_mobile']: mobile,
      ['form.apply_name']: realName,
      ['form.IDcard']: idcard,
      ['form.rider_type']: rider_type,
      ['form.prefecture']: prefecture
    });
  },

  onShow: function () {
		var that = this;
		that.setData({
			cdnUrl: app.cdnUrl,
		});
  },

  riderApply: function () {
    var that = this;
    if(!that.data.form.imageback) {
    	app.error('请上传身份证正面');
    	return false;
    }
    if(!that.data.form.imageback) {
    	app.error('请上传身份证反面');
    	return false;
    }
    app.request('/api/timetable.user/riderApply', that.data.form, function (data) {
      wx.removeStorageSync('mobile');
      wx.removeStorageSync('realName');
      wx.removeStorageSync('idcard');
      wx.removeStorageSync('rider_type');
      wx.removeStorageSync('prefecture');
      that.setData({
        applySuccess: false
      })
    }, function (data, ret) {
      app.error(ret.msg);
      return false;
    });
  },
  applySuccessHidden:function(){
    wx.reLaunch({
      url: '/page/index/index'
    })
  },
  //上传头像
  uploadImage: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;//0为身份证反面， 1为身份证正面
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var formData = app.globalData.config.upload.multipart;
        formData.token = app.globalData.userInfo.token;
        wx.uploadFile({
          url: app.globalData.config.upload.uploadurl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: formData,
          success: function (res) {
            var row = JSON.parse(res.data);
            if (row.code == 1) {
              app.success('上传成功');
              if(type==0){
                console.log("反面");
                that.setData({ ["form.imageback"]: row.data.url });
              }
              if(type==1){
                console.log("正面");
                that.setData({ ["form.imagefront"]: row.data.url });
              }
            }
          },
          error: function (e) {
            app.error('上传失败,请重试');
            return false;
          }
        });
      }, error: function () {
        app.error('上传失败,请重试');
        return false;
      }
    });
  }

})